#!/bin/sh

# Read the configuration in.
. /etc/clearwater/config

if [ -n "$local_ip" ] && [ -n "$home_domain" ] && [ -n "$public_ip" ]
then
  # Add square brackets around the address iff it is an IPv6 address
  bracketed_ip=$(/usr/share/clearwater/bin/bracket-ipv6-address $local_ip)

  # Set up local_ipv*, defaulting the entry that is not applicable.
  if /usr/share/clearwater/bin/is-address-ipv6 $local_ip
  then
    local_ipv4=127.0.0.1
    local_ipv6=$local_ip
  else
    local_ipv4=$local_ip
    local_ipv6=::1
  fi

  # Set up public_ipv4 if we're using an IPv4 address, or default it if not.
  if /usr/share/clearwater/bin/is-address-ipv6 $public_ip
  then
    public_ipv4=127.0.0.1
  else
    public_ipv4=$public_ip
  fi

  # Build the restund configuration file.
  CONF=/etc/clearwater/restund.conf
  cat >$CONF <<EOF
# restund.conf
# core
daemon                  yes
debug                   no
realm                   $home_domain
syncinterval            600
udp_listen              $bracketed_ip:3478
udp_sockbuf_size        524288
tcp_listen              $bracketed_ip:3478
# modules
module_path             /usr/share/clearwater/restund/lib
module                  stat.so
module                  httpdb.so
module                  binding.so
module                  auth.so
module                  turn.so
module                  syslog.so
module                  status.so
# auth
auth_nonce_expiry       3600
# turn
turn_max_allocations    512
turn_max_lifetime       600
turn_relay_addr         $local_ipv4
turn_relay_snat_addr    $public_ipv4
turn_relay_addr6        $local_ipv6
# Cope with broken TURN clients
turn_extended_channels  yes
# httpdb
httpdb_url              http://hs.$home_domain:8888/impi/%s/digest
# Relax TURN security to support buggy clients
httpdb_uri_workaround   yes
httpdb_uri_workaround_password  $turn_workaround
# syslog
syslog_facility         24
# status
status_udp_addr         127.0.0.1
status_udp_port         33000
status_http_addr        127.0.0.1
status_http_port        8080
EOF
fi

# Set up the monit configuration for restund
cat > /etc/monit/conf.d/restund.monit <<EOF
# Check the Restund process.

# Monitor the service's PID file and memory use. Include a / in the "matching" 
# expression to ensure that this doesn't pick up poll_restund.sh by accident.
check process restund_process matching /restund
  group restund

  start program = "/etc/init.d/restund start"
  stop program = "/etc/init.d/restund stop"
  restart program = "/etc/init.d/restund restart"

  # Check the service's resource usage, and abort the process if it's too high.
  if memory > 80% for 6 cycles then exec "/etc/init.d/restund abort"

# Check the restund process is responsive. This depends on the restund process (and so won't run
# unless the restund process is running)
check program poll_restund with path "/usr/share/clearwater/bin/poll_restund.sh"
  group restund
  depends on restund_process
  if status != 0 for 2 cycles then exec "/etc/init.d/restund abort'"
EOF
chmod 0644 /etc/monit/conf.d/restund.monit

# Force monit to reload its configuration
reload clearwater-monit || true
